<template>
  <common-search :col-layout="{xxl: 6, xl:8, lg: 8, md: 12, sm: 24, xs: 24}" v-model="query" :formItems="formItems" @search="search" @reset="reset"></common-search>
  <a-button type="primary" @click="exports" :loading="exportLoading" v-modulePerm="['accessoryPool:export']">
    <template #icon>
      <ExportOutlined />
    </template>
    导出
  </a-button>
  <common-table
      :columns="columns"
      :dataSource="dataList"
      v-model:localPagination="page"
      @paginationChange="onPageChange"
      v-model:loading="loading"
      :scroll="{ x: 'max-content'}"
      :slots="['productType']"
  >
    <template #productType="{record}">
      {{productType(record.productType)}}
    </template>
  </common-table>
</template>
<script>
export default {
  name:'inventoryPools-accessoryPool'
}
</script>
<script setup>
import { ref,onMounted } from "vue";
import CommonTable from "@/components/commonTable/index.vue";
import CommonSearch from "@/components/commonSearch/index.vue";
import {useSearchTable} from "@/hooks/table/useSearchTable";
import {getUserList} from "@/api/system/user.js";
import {getManyDictItems} from "@/api/system/dict.js";
import {getAccessoryPage} from "@/api/inventoryPools/accessoryPool.js";
import {message} from "ant-design-vue";
import {postFile} from "@utils/service/index.js";
const columns = [
    {
      "dictCode": "site",
      "dataIndex": "site",
      "title": "站点",
      "align": "center",
      "fieldType": "String"
    },
    {

      "dataIndex": "poolCode",
      "title": "库存池",
      "align": "center",
      "fieldType": "String"
    },
    {

      "dataIndex": "warehouseCode",
      "title": "仓库",
      "align": "center",
      "fieldType": "String"
    },
    {

      "dataIndex": "etaDate",
      "title": "到港ETA",
      "align": "center",
      "fieldType": "Date"
    },
    {

      "dataIndex": "productSku",
      "title": "平台SKU",
      "align": "center",
      "fieldType": "String"
    },
    {

      "dataIndex": "versionCode",
      "title": "版本",
      "align": "center",
      "fieldType": "String"
    },
    {

      "dataIndex": "warehouseSku",
      "title": "仓库SKU",
      "align": "center",
      "fieldType": "String"
    },
    {

      "dataIndex": "qty",
      "title": "库存数量",
      "align": "center",
      "fieldType": "Long"
    },
    {

      "dataIndex": "orderQty",
      "title": "待处理",
      "align": "center",
      "fieldType": "Long"
    },
    {

      "dataIndex": "pkgSize",
      "title": "分包数",
      "align": "center",
      "fieldType": "Long"
    },
    {

      "dictCode": "pdmProductType",
      "dataIndex": "productType",
      "title": "产品类型",
      "align": "center",
      "fieldType": "String"
    },
    {

      "dictCode": "deliveryType",
      "dataIndex": "deliveryType",
      "title": "物流类型",
      "align": "center",
      "fieldType": "String"
    }
  ]
const formItems = ref([
  {
    label:'站点',
    type:'select',
    field:'site',
    options:[]
  },
  {
    label:"库存池",
    type:'input',
    field: 'poolCode',
  },
  {
    label:'仓库',
    type:'input',
    field:'warehouseCode',
  },
  {
    label:"到港ETA",
    type:'rangePicker',
    field: 'time',
    showTime: false,
    defaultValue: [],
  },
  {
    label:'物流类型',
    type:'select',
    field:'deliveryType',
    options:[]
  },
  {
    label:"产品类型",
    type:'select',
    field: 'productType',
    options:[]
  },
  {
    label:'平台SKU',
    type:'input',
    field:'productSku',
  },
  {
    label:"仓库SKU",
    type:'input',
    field: 'warehouseSku',
  },
])
const selectedRowKeys = ref([])
const exportLoading = ref(false)
const productType = (value)=> {
  let arr = {
      "product": "正式产品",
      "parts": "配件",
      "virtual": "虚拟",
      "sample": "样品",
      "outer": "外部",
      "giga": "云仓",
      "part": "分包件",
    }
    return arr[value]
}
const { dataList, query, loading, page, search, reset, onPageChange } = useSearchTable({
  searchAPI: getAccessoryPage,
  queryFactory: () => ({
    status: '',
    columns:'createTime'
  }),
  beforeSearch: () => {
    //处理日期
    if(query.value.time){
      query.value.etaDateStart = query.value.time[0]
      query.value.etaDateEnd = query.value.time[1]
    }else{
      delete query.value.etaDateStart
      delete query.value.etaDateEnd
    }
  },
});
/**
 * 初始化字典数据
 * @returns {Promise<void>}
 */
const init = async () => {
  try{
    const res = await getManyDictItems({
      dictCodeList:"site,delivery_type,sku_type"
    })
    formItems.value[0].options = res.result.site
    formItems.value[4].options = res.result.delivery_type
    formItems.value[5].options = res.result.sku_type
  }catch (e){
    console.log(e)
  }
}
/**
 *  选择行变化事件
 * @param e {Array} 选择行的keys
 */
const onSelectChange = (e) => {
  selectedRowKeys.value = e
}
const exports = async () => {
  try{
    exportLoading.value = true
    const res = await postFile('/inventoryPool/part/export','post',query.value,'配件散件库存池.xlsx')
    message.success('导出成功')
  }catch (e) {
    message.error(e)
  }finally {
    exportLoading.value = false
  }
}
onMounted(()=>{
  init()
})
</script>
<style scoped>

</style>
